package pack1;

import java.io.IOException;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.CollaborationRequest;
import com.swim.entity.FriendshipRequest;
import com.swim.entity.SkillRequest;
import com.swim.session.CollaborationRequestRemote;
import com.swim.session.FriendshipRemote;
import com.swim.session.NewSkillRequestRemote;
import com.swim.session.RegisteredUserRemote;
import com.swim.session.SkillRemote;


public class Home extends HttpServlet {
	private static final long serialVersionUID = 1500350178396316664L;

	public Home() {
		super();
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/WEB-INF/noauth/index.jsp";
		Object auth = request.getSession().getAttribute("authenticated");
		Object ref = null;
		String authEmail = (String) request.getSession().getAttribute("authenticated-email");
		try {
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			if(auth != null && (boolean)auth) {
				url = "/WEB-INF/auth/index.jsp";
				ref = jndiContext.lookup("FriendshipBeanJNDI");
				FriendshipRemote f = (FriendshipRemote) ref;
				
				List<FriendshipRequest> requests = f.getReceivedFriendshipRequests(authEmail);
				request.setAttribute("friendreq", requests);
				
				ref = jndiContext.lookup("CollaborationRequestBeanJNDI");
				CollaborationRequestRemote c = (CollaborationRequestRemote) ref;
				
				List<CollaborationRequest> colreq = c.getCollaborationRequests(authEmail);
				request.setAttribute("collabreq", colreq);

				ref = jndiContext.lookup("RegisteredUserBeanJNDI");
				RegisteredUserRemote usr = (RegisteredUserRemote) ref;

				request.setAttribute("notifications", usr.getNotifications(authEmail));
				request.setAttribute("user", usr.getUserWithEmail(authEmail));
				
				Object admin = request.getSession().getAttribute("admin");
				if(admin != null && (boolean)admin) {
					Object ref1 = jndiContext.lookup("NewSkillRequestBeanJNDI");
					NewSkillRequestRemote r1 = (NewSkillRequestRemote) ref1;
					List<SkillRequest> skillRequests = r1.getSkillRequests();
					request.setAttribute("skillRequests", skillRequests);
				}
			}
			
			ref = jndiContext.lookup("SkillBeanJNDI");
			SkillRemote r = (SkillRemote) ref;
			
			List<String> skills = r.getSkillNames();
			request.setAttribute("skills", skills);
			
			RequestDispatcher dispatcher = request.getRequestDispatcher(url);
			dispatcher.forward(request, response); 
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/noauth/index.jsp");
			dispatcher.forward(request, response);
			return;
    	}
    	String idStr = request.getParameter("id");
    	if(idStr != null) {
			int id = Integer.parseInt(idStr);
    		InitialContext jndiContext;
			try {
				jndiContext = (InitialContext) Util.getInitialContext();
				Object ref = null;
				boolean accept = false;
				if("friendship".equals((String)request.getParameter("type"))) {
		    		ref = jndiContext.lookup("FriendshipBeanJNDI");
					FriendshipRemote f = (FriendshipRemote) ref;
					accept = Boolean.parseBoolean(request.getParameter("accept"));
					FriendshipRequest req = f.getFriendshipRequest(id);
					f.acceptFriendshipRequest(id,accept);
					if(accept && req.isDirect()) {
						response.sendRedirect("suggestions.jsp?via="+req.getEmailFrom().getEmail());
						return;
					}
				} else if("collaboration".equals(request.getParameter("type"))){
		    		ref = jndiContext.lookup("CollaborationRequestBeanJNDI");
					CollaborationRequestRemote c = (CollaborationRequestRemote) ref;
					accept = Boolean.parseBoolean(request.getParameter("accept"));
					c.acceptCollaborationRequest(id,accept);
				} else if("newskill".equals(request.getParameter("type"))) {
		    		ref = jndiContext.lookup("NewSkillRequestBeanJNDI");
					NewSkillRequestRemote ns = (NewSkillRequestRemote) ref;
					ns.acceptNewSkillRequest(id,false,"","");
				}
				response.sendRedirect("index.jsp");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
	}
}
